﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SMSRouter
{
    public partial class ModemSettings : Form
    {
        Controller controller;
        public ModemSettings(Controller controller)
        {
            InitializeComponent();
            this.controller = controller;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool res = controller.ConnectModem(this.cboPortName.Text, Convert.ToInt32(this.cboBaudRate.Text), 
                Convert.ToInt32(this.cboDataBits.Text), Convert.ToInt32(this.txtReadTimeOut.Text), 
                Convert.ToInt32(this.txtWriteTimeOut.Text));
            if (res)
                this.lblConnectionStatus.Text = "Connected at " + this.cboPortName.Text;
            bool pinStatus = controller.ManagePinCode("", false);
            if (pinStatus)
                lblPinStatus.Text = "PIN Code is correct";
            else
                lblPinStatus.Text = "PIN Code required";
        }

        private void ModemSettings_Load(object sender, EventArgs e)
        {
            try
            {
                #region Display all available COM Ports
                string[] ports = SerialPort.GetPortNames();

                // Add all port names to the combo box:
                foreach (string port in ports)
                {
                    this.cboPortName.Items.Add(port);
                }
                #endregion


                this.btnDisconnect.Enabled = false;
            }
            catch (Exception ex)
            {
                //ErrorLog(ex.Message);
            }
        }

        private void btnPIN_Click(object sender, EventArgs e)
        {
            bool pinStatus = controller.ManagePinCode(textBoxPinCode.Text);
            if (pinStatus)
                lblPinStatus.Text = "PIN Code is correct";
            else
                lblPinStatus.Text = "PIN Code required";
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            controller.DisconnectModem();
            lblConnectionStatus.Text = "Not Connected";
            lblPinStatus.Text = "PIN Code required";
        }
    }
}
